package com.yami.shop.search.common.listener;

import com.yami.shop.bean.enums.EsOperationType;
import com.yami.shop.bean.event.EsMysteryBoxUpdateEvent;
import com.yami.shop.search.common.service.EsMysteryBoxService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 获取es盲盒商品数据事件
 * @author wxf
 * @date 2023/6/2 17:24
 */
@Component("esMysteryBoxUpdateListener")
@AllArgsConstructor
public class EsMysteryBoxUpdateListener {

    private final EsMysteryBoxService esMysteryBoxService;

    @EventListener(EsMysteryBoxUpdateEvent.class)
    @Async
    public void defaultEsProductListener(EsMysteryBoxUpdateEvent event) throws InterruptedException {
        // 保证商品添加完成数据库查的到
        Thread.sleep(500);
        EsOperationType esOperationType = event.getEsOperationType();
        Long id = event.getId();
        List<Long> ids = event.getIds();
        // 保存
        if (Objects.equals(esOperationType, EsOperationType.SAVE)) {
            esMysteryBoxService.save(id);
        }
        // 更新
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE)) {
            esMysteryBoxService.update(id);
        }
        // 删除
        else if (Objects.equals(esOperationType, EsOperationType.DELETE)) {
            esMysteryBoxService.update(id);
        }
        // 批量保存
        else if (Objects.equals(esOperationType, EsOperationType.SAVE_BATCH)) {
            esMysteryBoxService.saveBatch(ids);
        }
        // 批量更新
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_BATCH)) {
            esMysteryBoxService.updateBatch(ids);
        }
        // 批量删除
        else if (Objects.equals(esOperationType, EsOperationType.DELETE_BATCH)) {
            esMysteryBoxService.updateBatch(ids);
        }
        // 更新商品销量
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_SOLD_NUM)) {
            esMysteryBoxService.updateSoldNum(id);
        }
        // 批量更新商品销量
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_SOLD_NUM_BATCH)) {
            esMysteryBoxService.updateSoldNumBatch(ids);
        }
        // 批量更新商品库存
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_ORDER_STOCK_NUM_BATCH)) {
            esMysteryBoxService.changeProdStockBatch(event.getBoxList());
        }
        // 批量更新下单单商品销量
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_ORDER_SOLD_NUM_BATCH)) {
            esMysteryBoxService.changeProdSoldBatch(event.getBoxList());
        }
        // 根据分类id，更新商品
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_BY_CATEGORY_ID)) {
            esMysteryBoxService.updateByCategoryId(id);
        }
        // 根据店铺分类id，更新商品
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_BY_SHOP_CATEGORY_ID)) {
            esMysteryBoxService.updateByShopCategoryId(id);
        }
        // 根据店铺id，更新商品
        else if (Objects.equals(esOperationType, EsOperationType.UPDATE_BY_SHOP_ID)) {
            esMysteryBoxService.updateByShopId(id);
        }

    }
}
